package org.mall.modules.shop.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mall.common.util.GeneratorSnowFlakeIDUtils;
import org.mall.convert.ShopConvert;
import org.mall.modules.pojo.shop.ShopDTO;
import org.mall.modules.pojo.shop.ShopDetailDTO;
import org.mall.modules.pojo.shop.ShopPageBO;
import org.mall.modules.shop.entity.Shop;
import org.mall.modules.shop.entity.ShopCategoryRelationship;
import org.mall.modules.shop.entity.ShopClerkRelationShip;
import org.mall.modules.shop.entity.ShopSellerRe;
import org.mall.modules.shop.mapper.ShopCategoryRelationshipMapper;
import org.mall.modules.shop.mapper.ShopMapper;
import org.mall.modules.shop.service.IShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 商铺
 * @Author: jeecg-boot
 * @Date:   2019-10-23
 * @Version: V1.0
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private ShopCategoryRelationshipMapper shopCategoryRelationshipMapper;

    /**
     * 保存商户商铺关联关系
     * @param shopId
     * @param sellerId
     * @return
     */
    @Override
    public int saveRelationshipTable(String shopId, String sellerId) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = format.format(new Date());
        return shopMapper.saveRelationshipTable(shopId, sellerId, dateTime);
    }

    /**
     * 逻辑删除商铺
     * @param id
     */
    @Override
    public void removeShopById(String id) {
        shopMapper.removeShopById(id);
    }

    /**
     * 批量逻辑删除商铺
     * @param asList
     * @return
     */
    @Override
    public int removeShopByIds(List<String> asList) {
        return shopMapper.removeShopByIds(asList);
    }

    @Override
    public void deleteAllRelationShip(String clerkId) {
         shopMapper.deleteAllRelationShip(clerkId);
    }

    @Override
    public List<ShopPageBO> getAllShopListOfSeller(Page page, String sellerId) {
        List<Shop> list= shopMapper.getAllShopListOfSeller(page,sellerId);
        return list.stream().map(o->{
            ShopPageBO temp=new ShopPageBO();
            BeanUtils.copyProperties(o,temp);
            if(o.getShopPhoto()!=null) {
                temp.setShopPhoto(Arrays.asList(o.getShopPhoto().split(",")));
            }
            return temp;
        }).collect(Collectors.toList());
    }

    @Override
    public ShopSellerRe getShopSellerShipByShopId(String id) {
        return shopMapper.getShopSellerShipByShopId(id);
    }

    @Override
    public void deleteSellerShopRelationByShopId(String shopId) {
        shopMapper.deleteSellerShopRelationByShopId(shopId);
    }

    @Override
    public boolean addSellerShopRelationShip(String id, String id1) {
        return shopMapper.addSellerShopRelationShip(id,id1);
    }

    @Override
    public List<ShopDTO> queryClerkBelongShops(String clerkId) {
        return shopMapper.queryClerkBelongShops(clerkId);
    }

    @Override
    public List<ShopDTO> getListOfStores(String userId) {
        return shopMapper.getListOfStores(userId);
    }
    @Override
    public void saveCategoryAndSpuId(long spuId, String categoryId, String shopId) {
        ShopCategoryRelationship relationship = new ShopCategoryRelationship();
        relationship.setId(GeneratorSnowFlakeIDUtils.nextId());
        relationship.setCategoryId(categoryId);
        relationship.setSpuId(spuId);
        relationship.setShopId(shopId);
        relationship.setCreateTime(new Date());
        shopCategoryRelationshipMapper.saveCategoryAndSpuId(relationship);
    }
    @Override
    public void deleteCategoryAndSpuId(long spuId) {
        shopCategoryRelationshipMapper.deleteCategoryAndSpuId(spuId);
    }

    @Override
    public void deleteCategoryAndSpuIds(List<String> spuIds) {
        shopCategoryRelationshipMapper.deleteCategoryAndSpuIds(spuIds);
    }


    @Override
    public ShopClerkRelationShip getShopSellerShipBySellerIdAndShopId(String userId, String shopId) {
        return shopMapper.getShopSellerShipBySellerIdAndShopId(userId,shopId);
    }
    /**
     * 通过店铺id获取店铺的信息
     * @param shopIds
     * @return
     */
    @Override
    public List<ShopDTO> getById(List<String> shopIds) {
        return shopMapper.getById(shopIds);
    }

    @Override
    public IPage<Shop> getListByCoordinate(Page page, String co) {
        return  shopMapper.getListByCoordinate(page,co);
    }

    @Override
    public Boolean updateShopStatus(String shopId, Integer status) {
        return shopMapper.updateShopStatus(shopId,status);
    }

    @Override
    public Boolean updateStatus(String shopId, Integer status) {
        return shopMapper.updateStatus(shopId,status);

    }

    @Override
    public List<ShopDetailDTO> getShopLisBySellerId(String userId) {
        List<ShopDTO> list= shopMapper.getShopLisBySellerId(userId);


       return ShopConvert.INSTANCE.convert3(list);
    }

    @Override
    public Boolean addShopIncome(String shopId, Integer income) {
        return shopMapper.addShopIncome(shopId,income);
    }

    @Override
    public List<ShopDTO> getByClerkIds(List<String> clerkIds) {
        return  shopMapper.getByClerkIds(clerkIds);
    }

    @Override
    public List<ShopPageBO> getShopPageListOfClerk(Page page, String userId) {
        List<Shop> list= shopMapper.getShopPageListOfCelrk(page,userId);
        return list.stream().map(o->{
            ShopPageBO temp=new ShopPageBO();
            BeanUtils.copyProperties(o,temp);
            if(o.getShopPhoto()!=null) {
                temp.setShopPhoto(Arrays.asList(o.getShopPhoto().split(",")));
            }
            return temp;
        }).collect(Collectors.toList());
    }

    @Override
    public Shop getDetailById(String shopId) {
        return shopMapper.getDetailById(shopId);
    }

    @Override
    public Integer getAllShopCount() {
        return shopMapper.getAllShopCount();
    }

    @Override
    public List<Shop> getAllYearAllShop(String timeStart, String timeEnt) {
        return shopMapper.getAllYearAllShop(timeStart,timeEnt);
    }

    @Override
    public List<Shop> getShopNameList(String shopName) {
        shopName='%'+shopName+'%';
        return shopMapper.getShopNameList(shopName);
    }


}
